home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 1 / CU Amiga Magazine CD-ROM Special Edition (1995)(EMAP Images)(GB)[Issue 1995-11].iso / Aminet / comm / tcp / ATCP_sdk_40_gc.lha / AmiTCP-4.0-gcc / netinclude / rpc / xdr.h < prev   
C/C++ Source or Header  |  1995-04-07  |  11KB  |  283 lines

  1. #ifndef RPC_XDR_H
  2. #define RPC_XDR_H
  3. /*
  4.  * $Id: xdr.h,v 4.1 1994/09/26 08:07:59 jraja Exp jraja $
  5.  *
  6.  * External Data Representation Serialization Routines.
  7.  *
  8.  * Copyright © 1994 AmiTCP/IP Group,
  9.  *                  Network Solutions Development Inc.
  10.  *                  All rights reserved.
  11.  *
  12.  */
  13. /* @(#)xdr.h    2.2 88/07/29 4.0 RPCSRC */
  14. /*      @(#)xdr.h 1.19 87/04/22 SMI      */
  15.  
  16. /*
  17.  * Copyright (C) 1984, Sun Microsystems, Inc.
  18.  */
  19.  
  20. #include <stdio.h>        /* for the FILE* */
  21. /*
  22.  * XDR provides a conventional way for converting between C data
  23.  * types and an external bit-string representation.  Library supplied
  24.  * routines provide for the conversion on built-in C data types.  These
  25.  * routines and utility routines defined here are used to help implement
  26.  * a type encode/decode routine for each user-defined type.
  27.  *
  28.  * Each data type provides a single procedure which takes two arguments:
  29.  *
  30.  *    bool_t
  31.  *    xdrproc(xdrs, argresp)
  32.  *        XDR *xdrs;
  33.  *        <type> *argresp;
  34.  *
  35.  * xdrs is an instance of a XDR handle, to which or from which the data
  36.  * type is to be converted.  argresp is a pointer to the structure to be
  37.  * converted.  The XDR handle contains an operation field which indicates
  38.  * which of the operations (ENCODE, DECODE * or FREE) is to be performed.
  39.  *
  40.  * XDR_DECODE may allocate space if the pointer argresp is null.  This
  41.  * data can be freed with the XDR_FREE operation.
  42.  *
  43.  * We write only one procedure per data type to make it easy
  44.  * to keep the encode and decode procedures for a data type consistent.
  45.  * In many cases the same code performs all operations on a user defined type,
  46.  * because all the hard work is done in the component type routines.
  47.  * decode as a series of calls on the nested data types.
  48.  */
  49.  
  50. /*
  51.  * Xdr operations.  XDR_ENCODE causes the type to be encoded into the
  52.  * stream.  XDR_DECODE causes the type to be extracted from the stream.
  53.  * XDR_FREE can be used to release the space allocated by an XDR_DECODE
  54.  * request.
  55.  */
  56. enum xdr_op {
  57.     XDR_ENCODE=0,
  58.     XDR_DECODE=1,
  59.     XDR_FREE=2
  60. };
  61.  
  62. /*
  63.  * This is the number of bytes per unit of external data.
  64.  */
  65. #define BYTES_PER_XDR_UNIT    (4)
  66. #define RNDUP(x)  ((((x) + BYTES_PER_XDR_UNIT - 1) / BYTES_PER_XDR_UNIT) \
  67.             * BYTES_PER_XDR_UNIT)
  68.  
  69. /*
  70.  * The XDR handle.
  71.  * Contains operation which is being applied to the stream,
  72.  * an operations vector for the paticular implementation (e.g. see xdr_mem.c),
  73.  * and two private fields for the use of the particular impelementation.
  74.  */
  75. typedef struct XDR {
  76.     enum xdr_op    x_op;        /* operation; fast additional param */
  77.     struct xdr_ops {
  78.         bool_t    (*x_getlong)(struct XDR *xdrs, long *longp);    /* get a long from underlying stream */
  79.         bool_t    (*x_putlong)(struct XDR *xdrs, long *longp);    /* put a long to " */
  80.         bool_t    (*x_getbytes)(struct XDR *xdrs, caddr_t addr, 
  81.                       u_int len);/* get some bytes from " */
  82.         bool_t    (*x_putbytes)(struct XDR *xdrs, caddr_t addr, 
  83.                       u_int len);/* put some bytes to " */
  84.         u_int    (*x_getpostn)(struct XDR *xdrs);/* returns bytes off from beginning */
  85.         bool_t  (*x_setpostn)(struct XDR *xdrs, u_int pos);/* lets you reposition the stream */
  86.         long *    (*x_inline)(struct XDR *xdrs, u_int len);/* buf quick ptr to buffered data */
  87.         void    (*x_destroy)(struct XDR *xdrs);    /* free privates of this xdr_stream */
  88.     } *x_ops;
  89.     caddr_t     x_public;    /* users' data */
  90.     caddr_t        x_private;    /* pointer to private data */
  91.     caddr_t     x_base;        /* private used for position info */
  92.     int        x_handy;    /* extra private word */
  93. } XDR;
  94.  
  95. /*
  96.  * A xdrproc_t exists for each data type which is to be encoded or decoded.
  97.  *
  98.  * The second argument to the xdrproc_t is a pointer to an opaque pointer.
  99.  * The opaque pointer generally points to a structure of the data type
  100.  * to be decoded.  If this pointer is 0, then the type routines should
  101.  * allocate dynamic storage of the appropriate size and return it.
  102.  * bool_t    (*xdrproc_t)(XDR *, caddr_t *);
  103.  */
  104. typedef    bool_t (* XDRFUN xdrproc_t)(XDR *, void *);
  105. /*
  106.  * xdr_string_t is for type casts in cases where a xdr function is called
  107.  * via a pointer. If the function happens to be xdr_string(), then it uses
  108.  * the last argument, which is passed as UINT_MAX, othervice the u_int is
  109.  * just ignored.
  110.  */
  111. typedef    bool_t (* XDRFUN xdr_string_t)(XDR *, void *, u_int);
  112.  
  113. /*
  114.  * Operations defined on a XDR handle
  115.  *
  116.  * XDR        *xdrs;
  117.  * long        *longp;
  118.  * caddr_t     addr;
  119.  * u_int     len;
  120.  * u_int     pos;
  121.  */
  122. #define XDR_GETLONG(xdrs, longp)            \
  123.     (*(xdrs)->x_ops->x_getlong)(xdrs, longp)
  124. #define xdr_getlong(xdrs, longp)            \
  125.     (*(xdrs)->x_ops->x_getlong)(xdrs, longp)
  126.  
  127. #define XDR_PUTLONG(xdrs, longp)            \
  128.     (*(xdrs)->x_ops->x_putlong)(xdrs, longp)
  129. #define xdr_putlong(xdrs, longp)            \
  130.     (*(xdrs)->x_ops->x_putlong)(xdrs, longp)
  131.  
  132. #define XDR_GETBYTES(xdrs, addr, len)            \
  133.     (*(xdrs)->x_ops->x_getbytes)(xdrs, addr, len)
  134. #define xdr_getbytes(xdrs, addr, len)            \
  135.     (*(xdrs)->x_ops->x_getbytes)(xdrs, addr, len)
  136.  
  137. #define XDR_PUTBYTES(xdrs, addr, len)            \
  138.     (*(xdrs)->x_ops->x_putbytes)(xdrs, addr, len)
  139. #define xdr_putbytes(xdrs, addr, len)            \
  140.     (*(xdrs)->x_ops->x_putbytes)(xdrs, addr, len)
  141.  
  142. #define XDR_GETPOS(xdrs)                \
  143.     (*(xdrs)->x_ops->x_getpostn)(xdrs)
  144. #define xdr_getpos(xdrs)                \
  145.     (*(xdrs)->x_ops->x_getpostn)(xdrs)
  146.  
  147. #define XDR_SETPOS(xdrs, pos)                \
  148.     (*(xdrs)->x_ops->x_setpostn)(xdrs, pos)
  149. #define xdr_setpos(xdrs, pos)                \
  150.     (*(xdrs)->x_ops->x_setpostn)(xdrs, pos)
  151.  
  152. #define    XDR_INLINE(xdrs, len)                \
  153.     (*(xdrs)->x_ops->x_inline)(xdrs, len)
  154. #define    xdr_inline(xdrs, len)                \
  155.     (*(xdrs)->x_ops->x_inline)(xdrs, len)
  156.  
  157. #define    XDR_DESTROY(xdrs)                \
  158.     if ((xdrs)->x_ops->x_destroy)             \
  159.         (*(xdrs)->x_ops->x_destroy)(xdrs)
  160. #define    xdr_destroy(xdrs)                \
  161.     if ((xdrs)->x_ops->x_destroy)             \
  162.         (*(xdrs)->x_ops->x_destroy)(xdrs)
  163.  
  164. /*
  165.  * Support struct for discriminated unions.
  166.  * You create an array of xdrdiscrim structures, terminated with
  167.  * a entry with a null procedure pointer.  The xdr_union routine gets
  168.  * the discriminant value and then searches the array of structures
  169.  * for a matching value.  If a match is found the associated xdr routine
  170.  * is called to handle that part of the union.  If there is
  171.  * no match, then a default routine may be called.
  172.  * If there is no match and no default routine it is an error.
  173.  */
  174. #define NULL_xdrproc_t ((xdrproc_t)0)
  175. struct xdr_discrim {
  176.     int    value;
  177.     xdrproc_t proc;
  178. };
  179.  
  180. /*
  181.  * In-line routines for fast encode/decode of primitve data types.
  182.  * Caveat emptor: these use single memory cycles to get the
  183.  * data from the underlying buffer, and will fail to operate
  184.  * properly if the data is not aligned.  The standard way to use these
  185.  * is to say:
  186.  *    if ((buf = XDR_INLINE(xdrs, count)) == NULL)
  187.  *        return (FALSE);
  188.  *    <<< macro calls >>>
  189.  * where ``count'' is the number of bytes of data occupied
  190.  * by the primitive data types.
  191.  *
  192.  * N.B. and frozen for all time: each data type here uses 4 bytes
  193.  * of external representation.
  194.  */
  195. #define IXDR_GET_LONG(buf)        ((long)ntohl((u_long)*(buf)++))
  196. #define IXDR_PUT_LONG(buf, v)        (*(buf)++ = (long)htonl((u_long)v))
  197.  
  198. #define IXDR_GET_BOOL(buf)        ((bool_t)IXDR_GET_LONG(buf))
  199. #define IXDR_GET_ENUM(buf, t)        ((t)IXDR_GET_LONG(buf))
  200. #define IXDR_GET_U_LONG(buf)        ((u_long)IXDR_GET_LONG(buf))
  201. #define IXDR_GET_SHORT(buf)        ((short)IXDR_GET_LONG(buf))
  202. #define IXDR_GET_U_SHORT(buf)        ((u_short)IXDR_GET_LONG(buf))
  203.  
  204. #define IXDR_PUT_BOOL(buf, v)        IXDR_PUT_LONG((buf), ((long)(v)))
  205. #define IXDR_PUT_ENUM(buf, v)        IXDR_PUT_LONG((buf), ((long)(v)))
  206. #define IXDR_PUT_U_LONG(buf, v)        IXDR_PUT_LONG((buf), ((long)(v)))
  207. #define IXDR_PUT_SHORT(buf, v)        IXDR_PUT_LONG((buf), ((long)(v)))
  208. #define IXDR_PUT_U_SHORT(buf, v)    IXDR_PUT_LONG((buf), ((long)(v)))
  209.  
  210. /*
  211.  * Function to free memory allocated by an xdr object
  212.  */
  213. void xdr_free(xdrproc_t proc, void * objp);
  214.  
  215. /*
  216.  * These are the "generic" xdr routines.
  217.  */
  218. extern bool_t XDRFUN    xdr_void(XDR * xdrs, void * dummy);
  219. extern bool_t XDRFUN    xdr_int(XDR * xdrs, int * ip);
  220. extern bool_t XDRFUN    xdr_u_int(XDR * xdrs, u_int * uip);
  221. extern bool_t XDRFUN    xdr_long(XDR * xdrs, long * lp);
  222. extern bool_t XDRFUN    xdr_u_long(XDR * xdrs, u_long * ulp);
  223. extern bool_t XDRFUN    xdr_short(XDR * xdrs, short * sp);
  224. extern bool_t XDRFUN    xdr_u_short(XDR * xdrs, u_short * usp);
  225. extern bool_t XDRFUN    xdr_bool(XDR * xdrs, bool_t * bp);
  226. extern bool_t XDRFUN    xdr_enum(XDR * xdrs, enum_t * ep);
  227. extern bool_t XDRFUN    xdr_array(XDR * xdrs, caddr_t * addrp, 
  228.                   u_int * sizep, u_int maxsize, 
  229.                   u_int elsize, xdrproc_t elproc);
  230. extern bool_t XDRFUN    xdr_bytes(XDR * xdrs, char ** cpp, u_int * sizep, 
  231.                   u_int maxsize);
  232. extern bool_t XDRFUN    xdr_opaque(XDR * xdrs, caddr_t cp, u_int cnt);
  233. extern bool_t XDRFUN    xdr_string(XDR * xdrs, char ** cpp, u_int maxsize);
  234. extern bool_t XDRFUN    xdr_union(XDR * xdrs, int * dscmp, char * unp,
  235.                   struct xdr_discrim * choices,
  236.                   xdrproc_t dfault);
  237. extern bool_t XDRFUN    xdr_char(XDR * xdrs, char * cp);
  238. extern bool_t XDRFUN    xdr_u_char(XDR * xdrs, u_char * ucp);
  239. extern bool_t XDRFUN    xdr_vector(XDR * xdrs, char * basep, u_int nelem, 
  240.                    u_int elemsize, xdrproc_t xdr_elem);
  241. extern bool_t XDRFUN    xdr_float(XDR *xdrs, float *fp);
  242. extern bool_t XDRFUN    xdr_double(XDR *xdrs, double *dp);
  243. extern bool_t XDRFUN    xdr_reference(XDR * xdrs, caddr_t * pp, u_int size,
  244.                       xdrproc_t proc);
  245. extern bool_t XDRFUN    xdr_pointer(XDR * xdrs, char ** objpp,
  246.                     u_int obj_size, xdrproc_t xdr_obj);
  247. extern bool_t XDRFUN    xdr_wrapstring(XDR * xdrs, char ** cpp);
  248.  
  249. /*
  250.  * Common opaque bytes objects used by many rpc protocols;
  251.  * declared here due to commonality.
  252.  */
  253. #define MAX_NETOBJ_SZ 1024 
  254. struct netobj {
  255.     u_int    n_len;
  256.     char    *n_bytes;
  257. };
  258. typedef struct netobj netobj;
  259. extern bool_t XDRFUN   xdr_netobj(XDR * xdrs, struct netobj * np);
  260.  
  261. /*
  262.  * These are the public routines for the various implementations of
  263.  * xdr streams.
  264.  */
  265. extern void   xdrmem_create(XDR * xdrs, caddr_t addr, u_int size,
  266.                 enum xdr_op op);    /* XDR using memory buffers */
  267. #ifdef USE_DOSIO
  268. extern void   xdrstdio_create(XDR * xdrs, BPTR file, 
  269.                   enum xdr_op op);    /* XDR using stdio library */
  270. #else
  271. extern void   xdrstdio_create(XDR * xdrs, FILE * file, 
  272.                   enum xdr_op op);    /* XDR using stdio library */
  273. #endif
  274. extern void   xdrrec_create(XDR * xdrs, u_int sendsize, u_int recvsize,
  275.                 void * tcp_handle,
  276.                 int (* readit)(void *, caddr_t, int),
  277.                 int (* writeit)(void *, caddr_t, int)); /* XDR pseudo records for tcp */
  278. extern bool_t xdrrec_endofrecord(XDR * xdrs, int sendnow);/* make end of xdr record */
  279. extern bool_t xdrrec_skiprecord(XDR * xdrs);/* move to beginning of next record */
  280. extern bool_t xdrrec_eof(XDR * xdrs);    /* true if no more input */
  281.  
  282. #endif /* !RPC_XDR_H */
  283.